<?php
namespace cosocket\system\base;

/** 数据模型基类
 * Model Class
 */
class Model
{
    public function __construct()
    {
    }

    public function init($data)
    {
        $properties = get_object_vars($this);
        foreach ($properties as $property => $defaultValue) {
            if (isset($data[$property])) {
                $value = $data[$property];
                // 根据默认值类型进行转换
                switch (gettype($defaultValue)) {
                    case 'integer':
                        $this->$property = intval($value);
                        break;
                    case 'double':
                    case 'float':
                        $this->$property = floatval($value);
                        break;
                    case 'string':
                        $this->$property = strval($value);
                        break;
                    case 'boolean':
                        $this->$property = boolval($value);
                        break;
                    case 'array':
                        $this->$property = json_encode($value);
                        break;
                    default:
                        $this->$property = $value;
                }
            }
        }
    }

    // 转换为数组
    public function toArray(): array
    {
        $result = array();
        $properties = get_object_vars($this);
        foreach ($properties as $key => $value) {
            switch (gettype($value)) {
                case 'integer':
                    $result[$key] = intval($value);
                    break;

                case 'double':
                case 'float':
                    $result[$key] = floatval($value);
                    break;

                case 'string':
                    $result[$key] = strval($value);
                    break;

                case 'boolean':
                    $result[$key] = boolval($value);
                    break;

                case 'array':
                    $result[$key] = $this->handleArrayValue($value);
                    break;

                case 'object':
                    $result[$key] = $this->handleObjectValue($value);
                    break;

                case 'NULL':
                    $result[$key] = null;
                    break;

                default:
                    $result[$key] = strval($value);
            }
        }

        return $result;
    }

    /**
     * 处理数组类型的值
     */
    protected function handleArrayValue(array $array): array
    {
        $result = [];
        foreach ($array as $key => $value) {
            if (is_object($value)) {
                $result[$key] = $this->handleObjectValue($value);
            } elseif (is_array($value)) {
                $result[$key] = $this->handleArrayValue($value);
            } else {
                $result[$key] = $value;
            }
        }
        return $result;
    }

    /**
     * 处理对象类型的值
     */
    protected function handleObjectValue(object $object): mixed
    {
        // 如果对象是 DateTime
        if ($object instanceof \DateTime) {
            return $object->format('Y-m-d H:i:s');
        }

        // 如果对象实现了 toArray 方法
        if (method_exists($object, 'toArray')) {
            return $object->toArray();
        }

        // 如果对象是 SuperModel 的子类
        if ($object instanceof self) {
            return $object->toArray();
        }

        // 其他对象转为字符串
        return strval($object);
    }
}